#include "stdafx.h"
#include "SDLInitializer.h"
#include "GameConfiguration.h"

using namespace std;

namespace ZeEngine
{
    SDLInitializer::SDLInitializer() : m_pRenderer(NULL), m_pWindow(NULL), m_pWindowTexture(NULL)
    {
    }

    bool SDLInitializer::InitializeSDL(const GameConfiguration& configuration)
    {
        if( (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) ) 
        { 
            LOG_ERROR("Could not initialize SDL: " + string(SDL_GetError()));
            return false;
        }

         //Initialize SDL_ttf
        if( TTF_Init() == -1 )
        {
            LOG_ERROR("SDL_ttf could not initialize! SDL_ttf Error: " + string(TTF_GetError()) );
            return false;
        }

        //Initialize PNG loading
		int imgFlags = IMG_INIT_PNG;
		if( !( IMG_Init( imgFlags ) & imgFlags ) )
		{
			LOG_ERROR( "SDL_image could not initialize! SDL_image Error: " + string(IMG_GetError()) );
		}

		srand( SDL_GetTicks() );

        if(!CreateScreen(configuration))
        {
            // In the event that the window could not be made...
            LOG_ERROR("Could not create window: " + string(SDL_GetError()));
            return false;
        }

        return true;
    }

    bool SDLInitializer::CreateScreen(const GameConfiguration& configuration)
    {
        int flags = configuration.GetFullScreen() ? SDL_WINDOW_FULLSCREEN : 0 ;

        m_pWindow = SDL_CreateWindow(   
                                        configuration.GetCaption().c_str(),          
                                        SDL_WINDOWPOS_UNDEFINED,           
                                        SDL_WINDOWPOS_UNDEFINED,           
                                        configuration.GetScreenWidth(),                              
                                        configuration.GetScreenHeight(),
                                        flags | SDL_WINDOW_ALLOW_HIGHDPI
                                    );

        if (m_pWindow)
        {
            m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, SDL_RENDERER_ACCELERATED);

            if (m_pRenderer)
            {            
                SDL_RenderSetLogicalSize(m_pRenderer, configuration.GetLogicalResolution().GetX(), configuration.GetLogicalResolution().GetY());

                //Set texture filtering to linear
                if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "linear" ) )
                {
	                LOG_ERROR( "Warning: Linear texture filtering not enabled!" );
                }

                m_pWindowTexture = SDL_CreateTexture(m_pRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, configuration.GetLogicalResolution().GetX(), configuration.GetLogicalResolution().GetY());

                if (!m_pWindowTexture)
                {
                    LOG_ERROR("Error creating rendering texture: " + string(SDL_GetError()));
                }
            }
            else
            {
                LOG_ERROR("Error creating renderer: " + string(SDL_GetError()));
            }
           
       }

        return m_pWindow != NULL;
    }

    void SDLInitializer::DestroySDL()
    { 
        SDL_DestroyWindow(m_pWindow);
        SDL_DestroyTexture(m_pWindowTexture);

        TTF_Quit();
        IMG_Quit();
        SDL_Quit();

        m_pWindow = 0;
        m_pWindowTexture = 0;
    }
}